package com.sinosoft.quick;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sinosoft.base.GenericDaoHibernate;

@Repository
public class QuickDaoHibernate extends GenericDaoHibernate<Quick, Long>
		implements QuickDao {

	public QuickDaoHibernate() {
		super(Quick.class);
	}

	public int getQuickCount(String sql) {
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createSQLQuery(sql);
		int count = Integer.parseInt(query.list().get(0).toString());
		session.close();
		return count;
	}

	@SuppressWarnings("unchecked")
	public List<Quick> getQuickListByPage(Quick quick, int first, int pagesize) {
		List<Quick> quicklist = null;
		String satelliteid = quick.getSatelliteid();
		String sensorid = quick.getSensorid();
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria c = session.createCriteria(Quick.class);
		c.setFirstResult(first);
		c.setMaxResults(pagesize);
		c.addOrder(Order.desc("archivetime"));
		if(satelliteid!=null&& !satelliteid.isEmpty())
			c.add(Restrictions.eq("satelliteid", satelliteid));
		if(sensorid!=null&& !sensorid.isEmpty())
			c.add(Restrictions.eq("sensorid", sensorid));
		quicklist = c.list();
		session.close();
		return quicklist;
	}

	public Quick getQuickById(String sql)
	{
		List<Quick> quicklist =getHibernateTemplate().find(sql);
		if(quicklist.size()>0)
			return quicklist.get(0);
		else
			return null;
	}

	public void deleteQuick(Quick quick)
	{
		getHibernateTemplate().delete(quick);
	}
}
